//
//  CameraWebView.m
//  wAudioShare
//
//  Created by Anson on 14-4-9.
//  Copyright (c) 2014年 赵帅. All rights reserved.
//

#import "CameraWebView.h"

@interface CameraWebView () <UIWebViewDelegate>
{
//    NSTimer *dateTimer;
}
@property (nonatomic, strong) IBOutlet UIWebView *webView;
@property (nonatomic, strong) IBOutlet UILabel *labelTime;
@property (nonatomic, strong) IBOutlet UIView *gestureView;

@end

@implementation CameraWebView
@synthesize dateTimer;

- (void)addGestureToView {
 
    UISwipeGestureRecognizer *swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self
                                                                                                 action:@selector(controlCamera:)];
    swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
    [self.gestureView addGestureRecognizer:swipeGestureRecognizer];
    
    UISwipeGestureRecognizer *swipeGestureRecognizer1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self                            action:@selector(controlCamera:)];
    swipeGestureRecognizer1.direction = UISwipeGestureRecognizerDirectionDown ;
    [self.gestureView addGestureRecognizer:swipeGestureRecognizer1];
    
    UISwipeGestureRecognizer *swipeGestureRecognizer2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self              action:@selector(controlCamera:)];
    swipeGestureRecognizer2.direction = UISwipeGestureRecognizerDirectionRight;
    [self.gestureView addGestureRecognizer:swipeGestureRecognizer2];
    
    UISwipeGestureRecognizer *swipeGestureRecognizer3 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(controlCamera:)];
    swipeGestureRecognizer3.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.gestureView addGestureRecognizer:swipeGestureRecognizer3];

    
}

- (void)controlCamera:(UISwipeGestureRecognizer *)recognizer
{
    NSString *directionJavascriopt = @"document.getElementsByTagName('input')[%d].click();";
    if (recognizer.direction == UISwipeGestureRecognizerDirectionDown) {
        directionJavascriopt = [NSString stringWithFormat:directionJavascriopt, 0];
    }else if (recognizer.direction == UISwipeGestureRecognizerDirectionUp) {
        directionJavascriopt = [NSString stringWithFormat:directionJavascriopt, 1];
    }else if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
        directionJavascriopt = [NSString stringWithFormat:directionJavascriopt, 3];
    }else if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) {
        directionJavascriopt = [NSString stringWithFormat:directionJavascriopt, 2];
    }
    
    [self.webView stringByEvaluatingJavaScriptFromString:directionJavascriopt];
    
}

- (void)loadRequest {
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.camURL]]];
    
     dateTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES];//使用timer定时，
    
    [self addGestureToView];
}

- (void)webViewDidFinishLoad:(UIWebView*)theWebView
{
    [self.webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"];
    
    [self.webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout='none';"];
}

- (void)dealloc {
    self.webView = nil;
    [dateTimer invalidate];
    dateTimer = nil;
}

#pragma mark -
- (void)timerFireMethod:(NSTimer*)theTimer
{
    NSDateFormatter *date = [[NSDateFormatter alloc] init];
    [date setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    self.labelTime.text = [date stringFromDate:[NSDate date]];
}

- (BOOL)isWebViewLoading {
    return self.webView.isLoading;
}
@end
